import pyttsx3
import time
import random

SLEEPSECONDS = 5
REPEAT = 3
def text_to_speech(text):
    # 初始化 pyttsx3 引擎
    engine = pyttsx3.init()
    # vs = engine.getProperty('voices')
    # print(vs)

    # 设置语速（可选）
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate - 50)
    engine.setProperty('voice', 'Huihui')

    # 设置音量（可选）
    volume = engine.getProperty('volume')
    engine.setProperty('volume', volume + 0.25)

    words = text.split()
    random.shuffle(words)
    i = 1
    result = []
    for word in words:
        tts = f'第{i}个：{word}'
        result.append(tts)
        for _ in range(REPEAT):  # 每个词读三遍
            engine.say(tts)
            engine.runAndWait()
            time.sleep(SLEEPSECONDS)  # 每遍之间间隔3秒
        i=i+1
    return result
if __name__ == "__main__":
    words = input("请输入几个词语（用空格分隔）：")
    # words = "青蛙 买卖 泉水"
    r= text_to_speech(words) #speak words and get the result
    for l in r: # print each line in result
        print(l)
